<?php
class PagingWidget extends CWidget {
    //Init in parameter of widget
    public $totalItem;
    public $amountPerPage;
    public $currentPage;

    public function init()
    {
        //This method is called at begin widget
    }//End function init

    public function run() 
    {
        //Init Param
        $i_totalPage     = 0;
        $i_startingIndex = 1;
        $i_endingIndex   = 1;

        //Calculate amount of page
        if( $this->totalItem % $this->amountPerPage == 0 ) {
            $i_totalPage = $this->totalItem / $this->amountPerPage;
        } else {
            $i_totalPage = (int)($this->totalItem / $this->amountPerPage) + 1;
        }

        //Set starting index of paging
        if ( ($this->currentPage - 4) > 1 ) {
            $i_startingIndex = $this->currentPage - 4;
        }

        //Set Ending Index of paging
        if ( ($this->currentPage + 4) < $i_totalPage ) {
            $i_endingIndex = $this->currentPage + 4;
        } else {
            $i_endingIndex = $i_totalPage;
        }

        $this->render(
            "paging",
            array(
                "i_startingIndex" => $i_startingIndex,
                "i_endingIndex"   => $i_endingIndex,
                "i_currentPage"   => $this->currentPage,
                "i_totalPage"     => $i_totalPage
            )
        );
    }//End function run
}//End class PagingWidget